// import com.alibaba.druid.sql.ast.SQLExpr_ptr;
// import com.alibaba.druid.sql.ast.SQLName;
// import com.alibaba.druid.sql.ast.SQLStatementImpl;
// import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr_ptr;
// import com.alibaba.druid.sql.visitor.SQLASTVisitor;

// import java.util.ArrayList;
// import java.util.List;

#include "SQLAnalyzeTableStatement.h"
#include "../../visitor/SQLASTVisitor.h"
#include "SQLExprTableSource.h"
#include "../../../Exception/UnsupportedOperationException.h"
#include "SQLPartitionRef.h"
#include "../../../utils/listTran.h"
#include "../expr/SQLIdentifierExpr.h"

SQLAnalyzeTableStatement::SQLAnalyzeTableStatement()
{
  tableSources = std::make_shared<std::list<SQLExprTableSource_ptr>>();
  columns = std::make_shared<std::list<SQLName_ptr>>();
  adbColumns = std::make_shared<std::list<SQLIdentifierExpr_ptr>>();
  adbColumnsGroup = std::make_shared<std::list<SQLIdentifierExpr_ptr>>();
}

// @Override
void SQLAnalyzeTableStatement::accept0(SQLASTVisitor_ptr visitor)
{
  if (visitor->visit(SharedObject(SQLAnalyzeTableStatement)))
  {
    SQLObject_list_ptr tmp1 = ListTran::tran_class<SQLExprTableSource, SQLObject>(tableSources);
    this->acceptChild(visitor, tmp1);
    this->acceptChild(visitor, partition);
    SQLObject_list_ptr tmp2 = ListTran::tran_class<SQLName, SQLObject>(columns);
    this->acceptChild(visitor, tmp2);
    this->acceptChild(visitor, adbSchema);
    SQLObject_list_ptr tmp3 = ListTran::tran_class<SQLIdentifierExpr, SQLObject>(adbColumns);
    this->acceptChild(visitor, tmp3);
    SQLObject_list_ptr tmp4 = ListTran::tran_class<SQLIdentifierExpr, SQLObject>(adbColumnsGroup);
    this->acceptChild(visitor, tmp4);
    this->acceptChild(visitor, adbWhere);
  }
  visitor->endVisit(SharedObject(SQLAnalyzeTableStatement));
}

SQLExprTableSource_list_ptr SQLAnalyzeTableStatement::getTables()
{
  return tableSources;
}

SQLExprTableSource_ptr SQLAnalyzeTableStatement::getTable()
{
  if (tableSources->empty())
  {
    return nullptr;
  }

  if (tableSources->size() == 1)
  {
    return *tableSources->begin();
  }

  throw new UnsupportedOperationException();
}

SQLName_list_ptr SQLAnalyzeTableStatement::getColumns()
{
  return columns;
}

void SQLAnalyzeTableStatement::setTable(SQLExprTableSource_ptr table)
{
  if (table != nullptr)
  {
    table->setParent(SharedObject(SQLAnalyzeTableStatement));
  }

  if (tableSources->empty())
  {
    if (table == nullptr)
    {
      return;
    }

    tableSources->push_back(table);
    return;
  }

  if (tableSources->size() == 1)
  {
    if (table == nullptr)
    {
      this->tableSources->clear();
    }
    else
    {
      auto it_tableSources = tableSources->begin();
      *it_tableSources = table;
      // tableSources.set(0, table);
    }
    return;
  }

  throw new UnsupportedOperationException();
}

SQLIdentifierExpr_ptr SQLAnalyzeTableStatement::getAdbSchema()
{
  return adbSchema;
}

void SQLAnalyzeTableStatement::setAdbSchema(SQLIdentifierExpr_ptr adbSchema)
{
  this->adbSchema = adbSchema;
}

SQLIdentifierExpr_list_ptr SQLAnalyzeTableStatement::getAdbColumns()
{
  return adbColumns;
}

void SQLAnalyzeTableStatement::setAdbColumns(SQLIdentifierExpr_list_ptr adbColumns)
{
  this->adbColumns = adbColumns;
}

SQLIdentifierExpr_list_ptr SQLAnalyzeTableStatement::getAdbColumnsGroup()
{
  return adbColumnsGroup;
}

void SQLAnalyzeTableStatement::setAdbColumnsGroup(SQLIdentifierExpr_list_ptr adbColumnsGroup)
{
  this->adbColumnsGroup = adbColumnsGroup;
}

SQLExpr_ptr SQLAnalyzeTableStatement::getAdbWhere()
{
  return adbWhere;
}

void SQLAnalyzeTableStatement::setAdbWhere(SQLExpr_ptr adbWhere)
{
  this->adbWhere = adbWhere;
}

void SQLAnalyzeTableStatement::setTable(SQLName_ptr table)
{
  this->setTable(SQLExprTableSource_ptr(new SQLExprTableSource(table)));
}

SQLPartitionRef_ptr SQLAnalyzeTableStatement::getPartition()
{
  return partition;
}

void SQLAnalyzeTableStatement::setPartition(SQLPartitionRef_ptr x)
{
  if (x != nullptr)
  {
    x->setParent(SharedObject(SQLAnalyzeTableStatement));
  }
  this->partition = x;
}

BOOL_ptr SQLAnalyzeTableStatement::isForColums()
{
  return forColums;
}

void SQLAnalyzeTableStatement::setForColums(BOOL_ptr forColums)
{
  this->forColums = forColums;
}

BOOL_ptr SQLAnalyzeTableStatement::isCacheMetadata()
{
  return cacheMetadata;
}

void SQLAnalyzeTableStatement::setCacheMetadata(BOOL_ptr cacheMetadata)
{
  this->cacheMetadata = cacheMetadata;
}

BOOL_ptr SQLAnalyzeTableStatement::isNoscan()
{
  return noscan;
}

void SQLAnalyzeTableStatement::setNoscan(BOOL_ptr noscan)
{
  this->noscan = noscan;
}

BOOL_ptr SQLAnalyzeTableStatement::isComputeStatistics()
{
  return computeStatistics;
}

void SQLAnalyzeTableStatement::setComputeStatistics(BOOL_ptr computeStatistics)
{
  this->computeStatistics = computeStatistics;
}
